

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.storages._redis &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.storages._redis</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.storages._redis 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">pickle</span>

<span class="kn">from</span> <span class="nn">optuna._experimental</span> <span class="kn">import</span> <span class="n">experimental</span>
<span class="kn">from</span> <span class="nn">optuna._imports</span> <span class="kn">import</span> <span class="n">try_import</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">distributions</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">exceptions</span>
<span class="kn">from</span> <span class="nn">optuna.storages._base</span> <span class="kn">import</span> <span class="n">DEFAULT_STUDY_NAME_PREFIX</span>
<span class="kn">from</span> <span class="nn">optuna.storages</span> <span class="kn">import</span> <span class="n">BaseStorage</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">StudyDirection</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">StudySummary</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">FrozenTrial</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">TrialState</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>


<span class="k">with</span> <span class="n">try_import</span><span class="p">()</span> <span class="k">as</span> <span class="n">_imports</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">redis</span>


<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>  <span class="c1"># NOQA</span>


<div class="viewcode-block" id="RedisStorage"><a class="viewcode-back" href="../../../reference/storages.html#optuna.storages.RedisStorage">[文档]</a><span class="nd">@experimental</span><span class="p">(</span><span class="s2">&quot;1.4.0&quot;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">RedisStorage</span><span class="p">(</span><span class="n">BaseStorage</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Storage class for Redis backend.</span>

<span class="sd">    Note that library users can instantiate this class, but the attributes</span>
<span class="sd">    provided by this class are not supposed to be directly accessed by them.</span>

<span class="sd">    Example:</span>

<span class="sd">        We create an :class:`~optuna.storages.RedisStorage` instance using</span>
<span class="sd">        the given redis database URL.</span>

<span class="sd">        .. code::</span>

<span class="sd">            &gt;&gt;&gt; import optuna</span>
<span class="sd">            &gt;&gt;&gt;</span>
<span class="sd">            &gt;&gt;&gt; def objective(trial):</span>
<span class="sd">            &gt;&gt;&gt;     ...</span>
<span class="sd">            &gt;&gt;&gt;</span>
<span class="sd">            &gt;&gt;&gt; storage = optuna.storages.RedisStorage(</span>
<span class="sd">            &gt;&gt;&gt;     url=&#39;redis://passwd@localhost:port/db&#39;,</span>
<span class="sd">            &gt;&gt;&gt; )</span>
<span class="sd">            &gt;&gt;&gt;</span>
<span class="sd">            &gt;&gt;&gt; study = optuna.create_study(storage=storage)</span>
<span class="sd">            &gt;&gt;&gt; study.optimize(objective)</span>

<span class="sd">    Args:</span>
<span class="sd">        url: URL of the redis storage, password and db are optional. (ie: redis://localhost:6379)</span>

<span class="sd">    .. note::</span>
<span class="sd">        If you use plan to use Redis as a storage mechanism for optuna,</span>
<span class="sd">        make sure Redis in installed and running.</span>
<span class="sd">        Please execute ``$ pip install -U redis`` to install redis python library.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url</span><span class="p">):</span>
        <span class="c1"># type: (str) -&gt; None</span>

        <span class="n">_imports</span><span class="o">.</span><span class="n">check</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span> <span class="o">=</span> <span class="n">redis</span><span class="o">.</span><span class="n">Redis</span><span class="o">.</span><span class="n">from_url</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">create_new_study</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># type: (Optional[str]) -&gt; int</span>

        <span class="k">if</span> <span class="n">study_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_name</span><span class="p">(</span><span class="n">study_name</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">DuplicatedStudyError</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;study_counter&quot;</span><span class="p">):</span>
            <span class="c1"># We need the counter to start with 0.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">&quot;study_counter&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">study_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">incr</span><span class="p">(</span><span class="s2">&quot;study_counter&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="c1"># We need the trial_number counter to start with 0.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:trial_number&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">study_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">study_name</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{}{:010d}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">DEFAULT_STUDY_NAME_PREFIX</span><span class="p">,</span> <span class="n">study_id</span><span class="p">)</span>

        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">pipeline</span><span class="p">()</span> <span class="k">as</span> <span class="n">pipe</span><span class="p">:</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">multi</span><span class="p">()</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_name</span><span class="p">(</span><span class="n">study_name</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:study_name&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_name</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span>
                <span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:direction&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span>
                <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">StudyDirection</span><span class="o">.</span><span class="n">NOT_SET</span><span class="p">),</span>
            <span class="p">)</span>

            <span class="n">study_summary</span> <span class="o">=</span> <span class="n">StudySummary</span><span class="p">(</span>
                <span class="n">study_name</span><span class="o">=</span><span class="n">study_name</span><span class="p">,</span>
                <span class="n">direction</span><span class="o">=</span><span class="n">StudyDirection</span><span class="o">.</span><span class="n">NOT_SET</span><span class="p">,</span>
                <span class="n">best_trial</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                <span class="n">user_attrs</span><span class="o">=</span><span class="p">{},</span>
                <span class="n">system_attrs</span><span class="o">=</span><span class="p">{},</span>
                <span class="n">n_trials</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                <span class="n">datetime_start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                <span class="n">study_id</span><span class="o">=</span><span class="n">study_id</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">rpush</span><span class="p">(</span><span class="s2">&quot;study_list&quot;</span><span class="p">,</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_summary</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">study_id</span>

    <span class="k">def</span> <span class="nf">delete_study</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">pipeline</span><span class="p">()</span> <span class="k">as</span> <span class="n">pipe</span><span class="p">:</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">multi</span><span class="p">()</span>
            <span class="c1"># Sumaries</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">lrem</span><span class="p">(</span><span class="s2">&quot;study_list&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="c1"># Trials</span>
            <span class="n">trial_ids</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_trials</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">trial_id</span> <span class="ow">in</span> <span class="n">trial_ids</span><span class="p">:</span>
                <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;trial_id:</span><span class="si">{:010d}</span><span class="s2">:frozentrial&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>
                <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;trial_id:</span><span class="si">{:010d}</span><span class="s2">:study_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:trial_list&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:trial_number&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="c1"># Study</span>
            <span class="n">study_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_study_name_from_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;study_name:</span><span class="si">{}</span><span class="s2">:study_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_name</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:study_name&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:direction&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:best_trial_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:params_distribution&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_key_study_name</span><span class="p">(</span><span class="n">study_name</span><span class="p">):</span>
        <span class="c1"># type: (str) -&gt; str</span>

        <span class="k">return</span> <span class="s2">&quot;study_name:</span><span class="si">{}</span><span class="s2">:study_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_name</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; str</span>

        <span class="k">return</span> <span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:study_summary&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_study_summary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">study_summary</span><span class="p">):</span>
        <span class="c1"># type: (int, StudySummary) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_summary</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_get_study_summary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; StudySummary</span>

        <span class="n">summary_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
        <span class="k">assert</span> <span class="n">summary_pkl</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">summary_pkl</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_del_study_summary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_key_study_direction</span><span class="p">(</span><span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; str</span>

        <span class="k">return</span> <span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:direction&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_study_direction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">direction</span><span class="p">):</span>
        <span class="c1"># type: (int, StudyDirection) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_direction</span><span class="p">(</span><span class="n">study_id</span><span class="p">)):</span>
            <span class="n">direction_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_direction</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="k">assert</span> <span class="n">direction_pkl</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
            <span class="n">current_direction</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">direction_pkl</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">current_direction</span> <span class="o">!=</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">NOT_SET</span> <span class="ow">and</span> <span class="n">current_direction</span> <span class="o">!=</span> <span class="n">direction</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;Cannot overwrite study direction from </span><span class="si">{}</span><span class="s2"> to </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">current_direction</span><span class="p">,</span> <span class="n">direction</span>
                    <span class="p">)</span>
                <span class="p">)</span>

        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">pipeline</span><span class="p">()</span> <span class="k">as</span> <span class="n">pipe</span><span class="p">:</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">multi</span><span class="p">()</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_direction</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">direction</span><span class="p">))</span>
            <span class="n">study_summary</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
            <span class="n">study_summary</span><span class="o">.</span><span class="n">direction</span> <span class="o">=</span> <span class="n">direction</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_summary</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">set_study_user_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, str, Any) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="n">study_summary</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
        <span class="n">study_summary</span><span class="o">.</span><span class="n">user_attrs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">study_summary</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_study_system_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, str, Any) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="n">study_summary</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
        <span class="n">study_summary</span><span class="o">.</span><span class="n">system_attrs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">study_summary</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_study_id_from_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_name</span><span class="p">):</span>
        <span class="c1"># type: (str) -&gt; int</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_name</span><span class="p">(</span><span class="n">study_name</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s2">&quot;No such study </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_name</span><span class="p">))</span>
        <span class="n">study_id_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_name</span><span class="p">(</span><span class="n">study_name</span><span class="p">))</span>
        <span class="k">assert</span> <span class="n">study_id_pkl</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">study_id_pkl</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_study_id_from_trial_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; int</span>

        <span class="n">study_id_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;trial_id:</span><span class="si">{:010d}</span><span class="s2">:study_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">study_id_pkl</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s2">&quot;No such trial: </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">study_id_pkl</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_study_name_from_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; str</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="n">study_name_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:study_name&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">study_name_pkl</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s2">&quot;No such study: </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">study_name_pkl</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_study_direction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; StudyDirection</span>

        <span class="n">direction_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:direction&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">direction_pkl</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s2">&quot;No such study: </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">direction_pkl</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_study_user_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; Dict[str, Any]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="n">study_summary</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">study_summary</span><span class="o">.</span><span class="n">user_attrs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_study_system_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; Dict[str, Any]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="n">study_summary</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">study_summary</span><span class="o">.</span><span class="n">system_attrs</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_key_study_param_distribution</span><span class="p">(</span><span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; str</span>

        <span class="k">return</span> <span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:params_distribution&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_study_param_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; Dict</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_param_distribution</span><span class="p">(</span><span class="n">study_id</span><span class="p">)):</span>
            <span class="n">param_distribution_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_param_distribution</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="k">assert</span> <span class="n">param_distribution_pkl</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
            <span class="k">return</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">param_distribution_pkl</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">_set_study_param_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">param_distribution</span><span class="p">):</span>
        <span class="c1"># type: (int, Dict) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_key_study_param_distribution</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_all_study_summaries</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; List[StudySummary]</span>

        <span class="n">study_summaries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">study_ids</span> <span class="o">=</span> <span class="p">[</span><span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span> <span class="k">for</span> <span class="n">sid</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">lrange</span><span class="p">(</span><span class="s2">&quot;study_list&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)]</span>
        <span class="k">for</span> <span class="n">study_id</span> <span class="ow">in</span> <span class="n">study_ids</span><span class="p">:</span>
            <span class="n">study_summary</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
            <span class="n">study_summaries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">study_summary</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">study_summaries</span>

    <span class="k">def</span> <span class="nf">create_new_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">template_trial</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># type: (int, Optional[FrozenTrial]) -&gt; int</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">template_trial</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_running_trial</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">trial</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">template_trial</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;trial_counter&quot;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">&quot;trial_counter&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>

        <span class="n">trial_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">incr</span><span class="p">(</span><span class="s2">&quot;trial_counter&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">trial_number</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">incr</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:trial_number&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
        <span class="n">trial</span><span class="o">.</span><span class="n">number</span> <span class="o">=</span> <span class="n">trial_number</span>
        <span class="n">trial</span><span class="o">.</span><span class="n">_trial_id</span> <span class="o">=</span> <span class="n">trial_id</span>

        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">pipeline</span><span class="p">()</span> <span class="k">as</span> <span class="n">pipe</span><span class="p">:</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">multi</span><span class="p">()</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">trial</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">&quot;trial_id:</span><span class="si">{:010d}</span><span class="s2">:study_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">rpush</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:trial_list&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">trial_id</span><span class="p">)</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

            <span class="n">pipe</span><span class="o">.</span><span class="n">multi</span><span class="p">()</span>
            <span class="n">study_summary</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
            <span class="n">study_summary</span><span class="o">.</span><span class="n">n_trials</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_study_trials</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="n">min_datetime_start</span> <span class="o">=</span> <span class="nb">min</span><span class="p">([</span><span class="n">t</span><span class="o">.</span><span class="n">datetime_start</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_trials</span><span class="p">(</span><span class="n">study_id</span><span class="p">)])</span>
            <span class="n">study_summary</span><span class="o">.</span><span class="n">datetime_start</span> <span class="o">=</span> <span class="n">min_datetime_start</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_summary</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="o">.</span><span class="n">is_finished</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_update_cache</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">trial_id</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_create_running_trial</span><span class="p">():</span>
        <span class="c1"># type: () -&gt; FrozenTrial</span>

        <span class="k">return</span> <span class="n">FrozenTrial</span><span class="p">(</span>
            <span class="n">trial_id</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span>  <span class="c1"># dummy value.</span>
            <span class="n">number</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span>  <span class="c1"># dummy value.</span>
            <span class="n">state</span><span class="o">=</span><span class="n">TrialState</span><span class="o">.</span><span class="n">RUNNING</span><span class="p">,</span>
            <span class="n">params</span><span class="o">=</span><span class="p">{},</span>
            <span class="n">distributions</span><span class="o">=</span><span class="p">{},</span>
            <span class="n">user_attrs</span><span class="o">=</span><span class="p">{},</span>
            <span class="n">system_attrs</span><span class="o">=</span><span class="p">{},</span>
            <span class="n">value</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
            <span class="n">intermediate_values</span><span class="o">=</span><span class="p">{},</span>
            <span class="n">datetime_start</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(),</span>
            <span class="n">datetime_complete</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_trial_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="c1"># type: (int, TrialState) -&gt; bool</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">state</span> <span class="o">==</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">RUNNING</span> <span class="ow">and</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">WAITING</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">trial</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="n">state</span>
        <span class="k">if</span> <span class="n">state</span><span class="o">.</span><span class="n">is_finished</span><span class="p">():</span>
            <span class="n">trial</span><span class="o">.</span><span class="n">datetime_complete</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">trial</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_update_cache</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">trial</span><span class="p">))</span>

        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">set_trial_param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_value_internal</span><span class="p">,</span> <span class="n">distribution</span><span class="p">):</span>
        <span class="c1"># type: (int, str, float, distributions.BaseDistribution) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="c1"># Check param distribution compatibility with previous trial(s).</span>
        <span class="n">study_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_study_id_from_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="n">param_distribution</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_param_distribution</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">param_name</span> <span class="ow">in</span> <span class="n">param_distribution</span><span class="p">:</span>
            <span class="n">distributions</span><span class="o">.</span><span class="n">check_distribution_compatibility</span><span class="p">(</span>
                <span class="n">param_distribution</span><span class="p">[</span><span class="n">param_name</span><span class="p">],</span> <span class="n">distribution</span>
            <span class="p">)</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>

        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">pipeline</span><span class="p">()</span> <span class="k">as</span> <span class="n">pipe</span><span class="p">:</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">multi</span><span class="p">()</span>
            <span class="c1"># Set study param distribution.</span>
            <span class="n">param_distribution</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">distribution</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_key_study_param_distribution</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">)</span>
            <span class="p">)</span>

            <span class="c1"># Set params.</span>
            <span class="n">trial</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_external_repr</span><span class="p">(</span><span class="n">param_value_internal</span><span class="p">)</span>
            <span class="n">trial</span><span class="o">.</span><span class="n">distributions</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">distribution</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">trial</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_trial_number_from_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; int</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">number</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_key_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; str</span>

        <span class="k">return</span> <span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:best_trial_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_best_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; FrozenTrial</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">)):</span>
            <span class="n">all_trials</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_trials</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">deepcopy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">all_trials</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">all_trials</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="ow">is</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">]</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">all_trials</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;No trials are completed yet.&quot;</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_study_direction</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span> <span class="o">==</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">:</span>
                <span class="n">best_trial</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">all_trials</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">best_trial</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">all_trials</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">_set_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">best_trial</span><span class="o">.</span><span class="n">number</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">best_trial_id_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>
            <span class="k">assert</span> <span class="n">best_trial_id_pkl</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
            <span class="n">best_trial_id</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">best_trial_id_pkl</span><span class="p">)</span>
            <span class="n">best_trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">best_trial_id</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">best_trial</span>

    <span class="k">def</span> <span class="nf">_set_best_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int, int) -&gt; None</span>

        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">pipeline</span><span class="p">()</span> <span class="k">as</span> <span class="n">pipe</span><span class="p">:</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">multi</span><span class="p">()</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>

            <span class="n">study_summary</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
            <span class="n">study_summary</span><span class="o">.</span><span class="n">best_trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_study_summary</span><span class="p">(</span><span class="n">study_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">study_summary</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_trial_param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">param_name</span><span class="p">):</span>
        <span class="c1"># type: (int, str) -&gt; float</span>

        <span class="n">distribution</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">distributions</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_internal_repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">param_name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">set_trial_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, float) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>

        <span class="n">trial</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">trial</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_update_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; None</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">study_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_study_id_from_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:best_trial_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">)):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_set_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="n">best_value_or_none</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
        <span class="k">assert</span> <span class="n">best_value_or_none</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="k">assert</span> <span class="n">trial</span><span class="o">.</span><span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="n">best_value</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">best_value_or_none</span><span class="p">)</span>
        <span class="n">new_value</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>

        <span class="c1"># Complete trials do not have `None` values.</span>
        <span class="k">assert</span> <span class="n">new_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_study_direction</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span> <span class="o">==</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">new_value</span> <span class="o">&gt;</span> <span class="n">best_value</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_set_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">new_value</span> <span class="o">&lt;</span> <span class="n">best_value</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_set_best_trial</span><span class="p">(</span><span class="n">study_id</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">)</span>

        <span class="k">return</span>

    <span class="k">def</span> <span class="nf">set_trial_intermediate_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">intermediate_value</span><span class="p">):</span>
        <span class="c1"># type: (int, int, float) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="n">frozen_trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">frozen_trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>
        <span class="n">frozen_trial</span><span class="o">.</span><span class="n">intermediate_values</span><span class="p">[</span><span class="n">step</span><span class="p">]</span> <span class="o">=</span> <span class="n">intermediate_value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">frozen_trial</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_trial_user_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, str, Any) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>
        <span class="n">trial</span><span class="o">.</span><span class="n">user_attrs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_trial_system_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (int, str, Any) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_trial_is_updatable</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>
        <span class="n">trial</span><span class="o">.</span><span class="n">system_attrs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_set_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; str</span>

        <span class="k">return</span> <span class="s2">&quot;trial_id:</span><span class="si">{:010d}</span><span class="s2">:frozentrial&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; FrozenTrial</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_trial_id</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>

        <span class="n">frozen_trial_pkl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>
        <span class="k">assert</span> <span class="n">frozen_trial_pkl</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">frozen_trial_pkl</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="c1"># type: (int, FrozenTrial) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">),</span> <span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">trial</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_del_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; None</span>

        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">pipeline</span><span class="p">()</span> <span class="k">as</span> <span class="n">pipe</span><span class="p">:</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">multi</span><span class="p">()</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s2">&quot;trial_id:</span><span class="si">{:010d}</span><span class="s2">:study_id&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span>
            <span class="n">pipe</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_get_study_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; List[int]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="n">study_trial_list_key</span> <span class="o">=</span> <span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:trial_list&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">tid</span><span class="p">)</span> <span class="k">for</span> <span class="n">tid</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">lrange</span><span class="p">(</span><span class="n">study_trial_list_key</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">get_all_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">deepcopy</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="c1"># type: (int, bool) -&gt; List[FrozenTrial]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

        <span class="n">trials</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">trial_ids</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_study_trials</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">trial_id</span> <span class="ow">in</span> <span class="n">trial_ids</span><span class="p">:</span>
            <span class="n">frozen_trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>
            <span class="n">trials</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">frozen_trial</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">deepcopy</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">trials</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">trials</span>

    <span class="k">def</span> <span class="nf">get_n_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">,</span> <span class="n">state</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># type: (int, Optional[TrialState]) -&gt; int</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">state</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_all_trials</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>

        <span class="k">return</span> <span class="nb">len</span><span class="p">([</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_trials</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="o">==</span> <span class="n">state</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">read_trials_from_remote_storage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_check_study_id</span><span class="p">(</span><span class="n">study_id</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_check_study_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study_id</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; None</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;study_id:</span><span class="si">{:010d}</span><span class="s2">:study_name&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s2">&quot;study_id </span><span class="si">{}</span><span class="s2"> does not exist.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study_id</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_check_trial_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_redis</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_key_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s2">&quot;study_id </span><span class="si">{}</span><span class="s2"> does not exist.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial_id</span><span class="p">))</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>